//
//  DDMeAdapt.m
//  DingDongZP
//
//  Created by 三秒文化 on 2016/11/2.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

#import "DDMeAdapt.h"

@interface DDMeAdapt() {
    
    NSDictionary *userInfo;// =
  /*
   @{
        @"class": @"HSFeedsViewController",
        @"property": @{
                        @"ID": @"1",
                        @"type": @"view" 
                    }
   };
   */
}


@end

@implementation DDMeAdapt




+(void)push:(NSDictionary *)params {
    

    NSString *classStr = [NSString stringWithFormat:@"%@",params[@"class"]];
    Class newClass = NSClassFromString(classStr);
    
    if (!newClass) { return; }
    
    id instance = [[newClass alloc] init];
    NSDictionary *propertys = params[@"property"];
    
    [propertys enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
       
        if ([DDMeAdapt checkIsExistPropertyWithInstance:instance verifyPropertyName:key]) {
            
            [instance setValue:obj forKey:key];
        }
    }];
    
    // 获取导航控制器
    UITabBarController *tabVC = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    LDRTRootNavVC *nav = tabVC.viewControllers.lastObject;
    [nav pushViewController:instance animated:true];
}


+ (BOOL)checkIsExistPropertyWithInstance:(id)instance verifyPropertyName:(NSString *)verifyPropertyName {
    
    unsigned int outCount = 0;
    // 获取对象里的属性列表
    objc_property_t *properties = class_copyPropertyList([instance class], &outCount);
    
    
    for (int i = 0; i < outCount; i++) {
        
        objc_property_t property =properties[i];
        NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
        //  is have
        if ([propertyName isEqualToString:verifyPropertyName]) {
            
            free(properties);
            return true;
        }
    }

    free(properties);
    return NO;
}
@end
